package com.mofang.powerdekorhelper.activity.customer;

import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.CustomerListAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.CustomerAchivesType;
import com.mofang.powerdekorhelper.model.CustomerList;
import com.mofang.powerdekorhelper.persenter.CustomerManagerPersenter;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.ParamsUtils;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.CustomerManagerView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class CustomerManagerActivity extends MvpActivity<CustomerManagerView, CustomerManagerPersenter> implements
        CustomerManagerView,
        TabLayout.OnTabSelectedListener,
        AdapterView.OnItemSelectedListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseRecycleViewList.MyRecycleOnScrollListener {

    @BindView(R.id.customer_manager_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.customer_manager_tab_nav)
    TabLayout tabLayout;

    @BindView(R.id.customer_search_edit)
    EditText searchEdit;

    @BindView(R.id.customer_check_spiner)
    Spinner spiner;

    @BindView(R.id.customer_manager_list)
    BaseRecycleViewList customerRecycle;

    @BindView(R.id.customer_refresh_layout)
    MySwipeRefreshLayout refreshLayout;

    CustomerListAdapter adapter;

    private int page = 1;
    private int pageSize = 10;
    private boolean ispage = true;
    private int sellerId;
    private String search;
    private int property = 0;
    private String customerStatus = "";

    private boolean isLoadAll;

    private LoadPrograss loadPrograss;

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_customer_manager_layout);
        initTitleBarWithback(mTitleBar, R.string.customer_archives);
        mTitleBar.setDividerColor(R.color.grey_midle);
        spiner.setOnItemSelectedListener(this);
        refreshLayout.setOnRefreshListener(this);
        customerRecycle.setListOnScrollListener(this);
        loadPrograss = new LoadPrograss(this);
    }

    @Override
    public void initData() {

        sellerId = SharePerforenceUtils.getInstance(this).getUserID();
        presenter.getarCustomerAchivesType();
        hideProgress();

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    presenter.getCustomerList(ParamsUtils.getCustomerListParams(page, pageSize, ispage, sellerId, getKeyWord(), property, customerStatus));
                }
                return false;
            }
        });
    }

    @Override
    public void setCustomerAchivesType(CustomerAchivesType type) {

        List<CustomerAchivesType.Data> datas = type.getData();

        tabLayout.removeAllTabs();

        for (CustomerAchivesType.Data data : datas) {

            tabLayout.addTab(tabLayout.newTab().setText(data.getTypeName()).setTag(data.getId()));
        }

        tabLayout.setOnTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        Integer flag = (Integer) tab.getTag();
        switch (flag) {

            case 0:
                customerStatus = "";
                break;

            case 1:
                customerStatus = "新客户";
                break;

            case 2:
                customerStatus = "订单客户";
                break;

            case 3:
                customerStatus = "活动客户";
                break;
        }

        presenter.getCustomerList(ParamsUtils.getCustomerListParams(page, pageSize, ispage, sellerId, getKeyWord(), property, customerStatus));
    }

    @OnClick({R.id.customer_search_btn})
    void onclick(View v) {
        switch (v.getId()) {
            case R.id.customer_search_btn:
                page = 1;
                presenter.getCustomerList(ParamsUtils.getCustomerListParams(page, pageSize, ispage, sellerId, getKeyWord(), property, customerStatus));
                break;
        }
    }

    @Override
    public void setCustomerList(CustomerList customerList) {

        if (page == 1) {
            adapter = new CustomerListAdapter(customerList.getResult().getData(), this, R.layout.customer_list_item_layout);
            customerRecycle.setAdapter(adapter);
        } else {
            adapter.addData(customerList.getResult().getData());
            if (customerList.getResult().getData().isEmpty()) {
                isLoadAll = true;
                page = 1;
            }
        }
        hideProgress();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        property = position;
        isLoadAll = false;
        presenter.getCustomerList(ParamsUtils.getCustomerListParams(page, pageSize, ispage, sellerId, getKeyWord(), property, customerStatus));
    }

    @Override
    public void onRefresh() {
        isLoadAll = false;
        page = 1;
        presenter.getCustomerList(ParamsUtils.getCustomerListParams(page, pageSize, ispage, sellerId, getKeyWord(), property, customerStatus));
    }

    @Override
    public void loadMore() {
        if (!isLoadAll) {
            // presenter.getCustomerList(ParamsUtils.getCustomerListParams(++page, pageSize, ispage, sellerId, getKeyWord(), property, customerStatus));
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        refreshLayout.hide();
        loadPrograss.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {
        toast("暂无更多数据");
        hideProgress();
    }

    @Override
    public void hideNullLayout() {
        hideProgress();
    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {
        hideProgress();
    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
        toast(errorInfo);
    }

    @Override
    public CustomerManagerPersenter initPresenter() {
        return new CustomerManagerPersenter();
    }

    private String getKeyWord() {
        search = searchEdit.getText().toString().trim();
        return search;
    }
}
